<template>
  <Form ref="formItem" :model="formItem" :label-width="120" :rules="ruleValidate">
    <FormItem label="学年名称" prop="schoolyear_id">
      <Select v-model="formItem.schoolyear_id">
        <Option
          v-for="item in schoolyearlist"
          :value="item.schoolyear_id"
          :key="item.schoolyear_id"
        >{{ item.schoolyear_name.zh }}</Option>
      </Select>
    </FormItem>
    <FormItem label="学期编号" prop="semester_code">
      <Input v-model="formItem.semester_code" placeholder="学期编号"></Input>
    </FormItem>
    <FormItem label="学期名称" prop="semester_name.zh">
      <Input v-model="formItem.semester_name.zh" placeholder="学期名称"></Input>
    </FormItem>
    <FormItem label="学期状态" prop="semester_status">
      <Select v-model="formItem.semester_status">
        <Option :value="0" :key="0">过去</Option>
        <Option :value="1" :key="1">当前</Option>
        <Option :value="2" :key="2">未来</Option>
      </Select>
    </FormItem>
    <FormItem label="学期开始日期" prop="start_at">
      <DatePicker
        type="date"
        :transfer="transfer"
        format="yyyy-MM-dd"
        v-model="formItem.start_at"
        placeholder="请选择学年开始日期"
        style="width:100%"
      ></DatePicker>
    </FormItem>
    <FormItem label="学期结束日期" prop="end_at">
      <DatePicker
        type="date"
        :transfer="transfer"
        format="yyyy-MM-dd"
        v-model="formItem.end_at"
        placeholder="请选择学年结束日期"
        style="width:100%"
      ></DatePicker>
    </FormItem>
    <FormItem label="教学周开始日期" prop="teaching_start_at">
      <DatePicker
        type="date"
        :transfer="transfer"
        v-model="start"
        @on-change="getstart"
        placeholder="请选择教学周开始日期"
        style="width:100%"
      ></DatePicker>
    </FormItem>
    <FormItem label="教学周结束日期" prop="teaching_end_at">
      <DatePicker
        type="date"
        :transfer="transfer"
        v-model="end"
        @on-change="getend"
        placeholder="请选择教学周结束日期"
        style="width:100%"
      ></DatePicker>
    </FormItem>
    <FormItem>
      <Button type="primary" @click="submit('formItem')" :loading="btnload">{{ infoId?'修改':'添加' }}</Button>
    </FormItem>
  </Form>
</template>

<script>
export default {
  props: {
    infoId: Number
  },
  data() {
    return {
      schoolyearlist: [],
      formItem: {
        schoolyear_code: "",
        status_flag: "",
        start_at: "",
        end_at: "",
        semester_name: {
          zh: "",
          mn: "",
          en: ""
        }
      },
      start: "",
      end: "",
      btnload: false,
      transfer: true,
      addUrl: "api/semester/v1/semester/create",
      editUrl: "api/semester/v1/semester/lists/",
      updateUrl: "api/semester/v1/semester/update/",
      schoolyearUrl: "api/semester/v1/schoolyear/lists",
      ruleValidate: {
        "semester_name.zh": [
          { required: true, message: "请输入学期名称", trigger: "blur" }
        ],
        semester_code: [
          { required: true, message: "请输入学期名称", trigger: "blur" }
        ],
        semester_status: [
          {
            required: true,
            type: "number",
            message: "请选择学期状态",
            trigger: "change"
          }
        ],
        schoolyear_id: [
          {
            required: true,
            type: "number",
            message: "请选择学年",
            trigger: "change"
          }
        ],
        start_at: [
          {
            required: true,
            type: "date",
            message: "请选择学期开始日期",
            trigger: "change"
          }
        ],
        end_at: [
          {
            required: true,
            type: "date",
            message: "请选择学期结束日期",
            trigger: "change"
          }
        ],
        teaching_start_at: [
          { required: true, message: "请选择教学周开始日期", trigger: "blur" }
        ],
        teaching_end_at: [
          { required: true, message: "请选择教学周结束日期", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    /************************************************************
     * 提交
     ************************************************************/
    submit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.btnload = true;
          if (this.infoId != null) {
            this.editSubmit();
          } else {
            this.addSubmit();
          }
        } else {
          this.$Message.error("验证失败");
          this.btnload = false;
        }
      });
    },

    /************************************************************
     * 添加数据
     ************************************************************/
    addSubmit() {
      this.$http
        .post(this.addUrl, this.formItem)
        .then(val => {
          this.$Message.success(val.message);
          this.$emit("requery", true);
        })
        .catch(err => {
          if (err.code && err.code == 422) {
            this.$Message.error(err.message);
          }
          this.btnload = false;
        });
    },

    /************************************************************
     * 修改数据
     ************************************************************/
    editSubmit() {
      this.$http
        .put(this.updateUrl + this.infoId, this.formItem)
        .then(val => {
          this.$Message.success(val.message);
          this.$emit("requery", true);
        })
        .catch(err => {
          if (err.code && err.code == 422) {
            this.$Message.error(err.message);
          }
          this.btnload = false;
        });
    },

    /************************************************************
     * 单条数据查询
     ************************************************************/
    infodata() {
      this.$http
        .get(this.editUrl + this.infoId)
        .then(val => {
          let infoOld = JSON.parse(JSON.stringify(val.data));
          this.formItem.schoolyear_id = parseInt(infoOld.schoolyear_id);
          this.formItem.semester_code = infoOld.semester_code;
          this.formItem.semester_name = infoOld.semester_name;
          this.formItem.semester_status = parseInt(infoOld.semester_status);
          this.formItem.start_at = infoOld.start_at;
          this.formItem.end_at = infoOld.end_at;
          this.formItem.teaching_start_at = infoOld.teaching_start_at;
          this.formItem.teaching_end_at = infoOld.teaching_end_at;
          this.start = infoOld.teaching_start_at;
          this.end = infoOld.teaching_end_at;
        })
        .catch(err => {
          console.log(err);
        });
    },

    /************************************************************
     * 获取学年信息
     ************************************************************/
    schoolyear() {
      this.$http
        .get(this.schoolyearUrl)
        .then(val => {
          //TODO 修改val.data为val.data.data;
          this.schoolyearlist = val.data.data;
        })
        .catch(err => {
          this.$Message.error("学年获取失败！");
        });
    },
    /************************************************************
     * 处理教学周开始、结束时间
     ************************************************************/
    getstart(val) {
      this.formItem.teaching_start_at = val;
    },
    getend(val) {
      this.formItem.teaching_end_at = val;
    }
  },
  created() {
    this.schoolyear();
    if (this.infoId != null) {
      this.infodata();
    }
  }
};
</script>